const db = require('../config/db')

class Admin {
    // 注册账号
    register(admin_name,admin_email,admin_phone,admin_pwd,admin_create_time){
        return new Promise((resolve,reject)=>{
             db.query(`select admin_email from db_admin where admin_email = '${admin_email}'`)
             .then(
                 rows=>{
                     if(rows.length === 0) {
                        let sql = `insert into db_admin(admin_name,admin_email,admin_phone,admin_pwd,admin_create_time) 
                        values('${admin_name}','${admin_email}','${admin_phone}','${admin_pwd}','${admin_create_time}')`;
                        db.query(sql)
                        .then(
                             content=>{
                                 resolve({
                                    code: 200,
                                    msg: "注册成功。",
                                    admin_email,
                                    content
                                 })
                             },
                             err=>{
                                 reject({
                                    code: 500,
                                    msg: "注册失败。",
                                    err
                                 })
                             }
                        )
                     }
                     else {
                        resolve({
                            code: 1,
                            msg: "账号已经存在。",
                            admin_email
                        })
                     }
                 },
                 err=>{
                     reject({
                        code: 500,
                        msg:"查询账号信息失败。",
                        err 
                     })
                 }
             )
             
        })
    }
    // 登录账号
    login(admin_email){
        return new Promise((resolve,reject)=>{
            db.query(`select * from db_admin where admin_email = '${admin_email}'`)
            .then(
                rows=>{
                    if(rows && rows.length === 1) {
                        // 把用户密码记录着 [{admin_pwd:'xxxxxadfadfadsadfa'}]
                        resolve(rows[0])
                    }
                    else {
                        resolve({
                            cod: 404,
                            msg: "该账号不存在",
                            rows 
                        })
                    }
                },
                err=> {
                    reject({
                        code: 500,
                        err,
                        msg: "服务端异常。"
                    })
                }
            )
        })
    }
    // 获取管理员信息
    info(id,admin_email){
        return new Promise((resolve,reject)=>{
            db.query(`select admin_name,admin_email,admin_phone,admin_create_time from db_admin where id = '${id}' and admin_email = '${admin_email}'`)
            .then(
                rows=>{
                     if(rows.length === 1) {
                         resolve({
                            code: 200,
                            msg: "获取用户信息成功。",
                            data: rows[0]
                         })
                     }
                     else {
                        resolve({
                            code: 404,
                            msg: "该账号不存在。",
                            rows
                        })
                     }
                },
                err=> {
                    reject({
                        code: 500,
                        msg: "服务端发生异常。"
                    })
                }
            )
        })
    }
    // 找回密码
    findpwd(admin_email,new_admin_pwd){
        return new Promise((resolve,reject)=>{
            db.query(`select admin_email from db_admin where admin_email = '${admin_email}'`)
            .then(
                 rows=>{
                     if(rows.length === 0) {
                        resolve({
                            code: 404,
                            msg: "该账号不存在。"
                         })
                     }
                     else 
                     if(rows.length === 1) {
                             // 修改用户名，密码
                             let sql = `update db_admin set admin_pwd = '${new_admin_pwd}' where  admin_email = '${admin_email}'`;
                            db.query(sql)
                                .then(
                                    content => {

                                        resolve({
                                            code: 200,
                                            msg: "重置管理员密码成功。",
                                            content
                                        })
                                    },
                                    err => {
                                        reject({
                                            code: 500,
                                            msg: "重置管理员密码失败。",
                                            err
                                        })
                                    }
                                )
                     }
                     else {
                         resolve({
                            code: 403,
                            msg: "请联系管理员。"
                         })
                     }
                 }
            )
        })
    }

    // 管理用户模块
    // 删除账号
    remove_user(user_id) {
        return new Promise((resolve, reject) => {
            // 查询账号
            let sql = `select * from db_user where  id='${user_id}'`;
            db.query(sql)
                .then(
                    (rows) => {
                        if (rows.length == 1) {
                            // 删除数据表的sql语句
                            let sql2 = `delete from db_user where id = '${user_id}'`
                            db.query(sql2).then(
                                (result) => {
                                    if(result.affectedRows === 1){
                                        resolve({
                                            code: 200,
                                            msg: '删除账号成功。',
                                        })
                                    }
                                    else {
                                        resolve({
                                            code: -1,
                                            msg: '删除账号失败。',
                                        })
                                    }
                                },
                                err => {
                                    reject(err)
                                }
                            )
                        }
                        else {
                            resolve({
                                code: 404,
                                msg: '该账户不存在。'
                            })
                        }
                    },
                    (err) => {
                        reject({
                            code: -1,
                            msg: '查询账号失败。'
                        })
                    }
                )
        })
    }

    // 用户列表
    list_user(page, pageSize) {
        return new Promise((resolve, reject) => {
            //获取分页数据
            let page_num = ((parseInt(page) - 1) * parseInt(pageSize))
            let page_size = (parseInt(pageSize))
            let sql = `select * from db_user limit ${page_num}, ${page_size}`;
            let sql2 = `select count(*) as total from db_user`
            // resolve({page_num, page_size})
            db.query(sql)
                .then(
                    (rows) => {
                        let data;
                        db.query(sql2)
                            .then(
                                (obj) => {
                                    data = obj
                                    resolve({
                                        code: 200,
                                        msg: '获取用户列表成功。',
                                        result: rows,
                                        page,
                                        pageSize,
                                        data
                                    })
                                }
                            )
                    },
                    err => {
                        reject({
                            code: -1,
                            msg: '获取用户列表失败。',
                            err
                        })
                    }
                )
        })
    }

    // 搜索用户
    search(keyword) {
        return new Promise((resolve,reject)=>{
            let sql = `select * from db_user where user_name like '%${keyword}%' or user_phone like '%${keyword}%'`;
            db.query(sql)
            .then(
                (rows)=>{
                    resolve({
                        code: 200,
                        msg: `关键字“${keyword}”搜索的结果`,
                        result: rows 
                    })
                },
                (err)=>{
                    reject({
                        code: -1,
                        msg: '关键字搜索失败。'
                    })
                },
            )
        })
    }


    foo(){
        return new Promise((resolve,reject)=>{

        })
    }
}


module.exports = new Admin();